package com.doucome.app.idiom 
{
	import com.adobe.serialization.json.JSON;
	import fl.controls.TextInput;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;

	
	/**
	 * ...
	 * @author wkl
	 */
	public class QuestionView extends MovieClip 
	{
		private var _pic_loader:Loader;
		private var _bg_pic:MovieClip;
		private var _btn_submit:SimpleButton;
		private var _input_answer:TextInput;
		private var _score:int;
		public static const QUESTION_RIGHT_EVENT:String = "question_right_event";
		public static const QUESTION_WRONG_EVENT:String = "question_wrong_event";
		
		public function QuestionView() 
		{
			_bg_pic = getChildByName("bg_pic") as MovieClip;
			_input_answer = getChildByName("input_answer") as TextInput;
			_btn_submit = getChildByName("btn_submit") as SimpleButton;
			_btn_submit.addEventListener(MouseEvent.CLICK, onSubmit);
			_pic_loader = new Loader();
			var pic_url = "http://static.app.doucome.com/sample.jpg";
			
			_pic_loader.load(new URLRequest(pic_url));
			_bg_pic.addChild(_pic_loader);
		}
		
		private function onSubmit(e:MouseEvent):void 
		{
			var rq:URLRequest = new URLRequest("http://dev.doucome.com/idiom/");
			//trace(_input_answer.text);
			var vars:URLVariables = new URLVariables("a=judge&answer="+_input_answer.text);
			rq.data = vars;
			var ld = new URLLoader();
			//ld.addEventListener(Event.OPEN, onSyncStart);
			ld.addEventListener(Event.COMPLETE, onSubmitComplete);
			//ld.addEventListener(IOErrorEvent.IO_ERROR, onSyncError);
			ld.load(rq);
		}
		
		private function onSubmitComplete(e:Event):void 
		{
			//trace(e.target.data);
			var result:Array = com.adobe.serialization.json.JSON.decode(e.target.data);
				_score = result[0].score;
				trace(_score);
		}
		
	}

}